import Vue from 'vue';
import VueRouter from 'vue-router';
import {
    checkSession
} from '../api/login';
Vue.use(VueRouter);
const routes = [{
        path: '/',
        redirect: '/login'
    },
    {
        path: '/',
        component: () => import('../components/common/Home.vue'),
        meta: {
            title: '自述文件'
        },
        children: [{
                path: '/home',
                component: () => import('../components/page/dashboard/index.vue'),
                meta: {
                    title: '首页'
                }
            },
            {
                path: '/verification',
                component: () => import('../components/page/verification/index.vue'),
                meta: {
                    title: '核验管理'
                }
            },
            {
                path: '/digitalcertificate',
                component: () => import('../components/page/digitalcertificate/index.vue'),
                meta: {
                    title: '数字凭证'
                }
            },
            {
                path: '/month',
                component: () => import('../components/page/present/month.vue'),
                meta: {
                    title: '上报呈送月报',
                }
            },
            {
                path: '/season',
                component: () => import('../components/page/present/season.vue'),
                meta: {
                    title: '上报呈送季报',
                }
            },
            {
                name: 'ViewPresent',
                path: '/presentView',
                component: {
                    render(c) {
                        return c('router-view')
                    }
                },
                hidden: true,
                component: () => import('../components/page/present/view.vue'),
                meta: {
                    title: '上报呈送详情',
                }
            },
            {
                path: '/databigscreen',
                beforeEnter() {window.open("https://sz.openviewtech.com/screen/index.html", '_blank');},
                // component: () => import('../components/page/databigscreen/index.vue'),
                meta: {
                    title: '数据大屏'
                }
            },

            {
                path: '/resident',
                component: () => import('../components/page/resident/index.vue'),
                meta: {
                    title: '居民管理'
                }
            },
            {
                path: '/user',
                component: () => import('../components/page/Users.vue'),
                meta: {
                    title: '人员管理'
                }
            },
            {
                path: '/permission',
                component: () => import('../components/page/Role.vue'),
                meta: {
                    title: '权限管理'
                }
            },

            // {
            //     path: '/suggest',
            //     component: () => import('../components/page/Suggest.vue'),
            //     meta: { title: '意见反馈' }
            // },
        ]
    },
    {
        path: '/login',
        component: () => import('../components/page/Login.vue'),
        meta: {
            title: '登录'
        }
    },
    {
        path: '/screen',
        component: () => import('../components/page/screen/index.vue'),
        meta: {
            title: '数据大屏'
        }
    },
    {
        path: '/loginmsg',
        component: () => import('../components/page/Loginmsg.vue'),
        meta: {
            title: '授权失败'
        }
    },
    {
        path: '/loginszzx',
        component: () => import('../components/page/Loginszzx.vue'),
        meta: {
            title: '社智在线联合登录'
        }
    },

    {
        path: '/auth/callback',
        component: () => import('../components/page/Login.vue'),
        meta: {
            title: '免登陆'
        }
    },
];
const router = new VueRouter({
    mode: 'history',
    routes
});

//钩子函数，拦截所有routes中的请求
router.beforeEach((to, from, next) => {
    document.title = `${to.meta.title} | 后台管理系统`;
    //判断token
  

    const mUrl = to.fullPath.split("?")
   
    if(mUrl[0] != '/login' && !to.fullPath.endsWith('loginszzx') && !to.fullPath.endsWith('loginmsg')){
        checkSession()
        .then((res) => {
            if(res.code === 10001){
                localStorage.removeItem('ms_username');
                localStorage.removeItem('token');
                localStorage.removeItem('userPermissions');
                localStorage.removeItem('level');
                localStorage.removeItem('superManager');
                localStorage.removeItem('openKey');
                next('/loginmsg')
                return false
            }
        })
    }
    const list = []
    if (mUrl[0] == '/auth/callback') {
        const kv = mUrl[1].split("&")

        for (var i = 0; i < kv.length; i++) {
            const mode = {}
            const op = kv[i].split("=")
            const value = op[1]
            const key = op[0]
            mode.key = key
            mode.value = value
            list.push(mode)
        }
    }
    if (mUrl[0] != '/auth/callback' && localStorage.getItem('token') != null || to.fullPath.endsWith('login') || to.fullPath.endsWith('loginszzx') || to.fullPath.endsWith('loginmsg')) {
        next()
    } else {
        if (mUrl[0] == '/auth/callback') {
            localStorage.removeItem('ms_username');
            localStorage.removeItem('token');
            localStorage.removeItem('userPermissions');
            localStorage.removeItem('level');
            localStorage.removeItem('superManager');
            localStorage.removeItem('openKey');
            localStorage.setItem('openKey', list[1]['value'])
            localStorage.setItem('newUrl', list[0]['value'])
            next('/loginszzx')

        }else{
   
            // 没登录则跳转到登录界面
            next('/login')
        }
        // next('/login')

    }
});
export default router;